﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeTop.复习九月份
{
    public class Solution102
    {
        public IList<IList<int>> LevelOrder(TreeNode root)
        {
            IList<IList<int>> res = new List<IList<int>>();
            Queue<TreeNode> queue = new Queue<TreeNode>();

            if (root == null)
            {
                return res;
            }
            queue.Enqueue(root);
            while (queue.Count > 0)
            {
                int count = queue.Count;
                IList<int> path = new List<int>();
                while (count > 0)
                {
                    TreeNode node = queue.Dequeue();
                    Console.WriteLine(node.val);
                    path.Add(node.val);
                    count--;

                    if (node.left != null)
                    {
                        queue.Enqueue(node.left);
                    }
                    if (node.right != null)
                    {
                        queue.Enqueue(node.right);
                    }
                }
                res.Add(path);
            }
            return res;
        }
    }
}